package com.situ.web.servlet;

import com.situ.web.dao.IStudentDao;
import com.situ.web.dao.ITeacherDao;
import com.situ.web.dao.impl.StudentDaoImpl;
import com.situ.web.dao.impl.TeacherDaoImpl;
import com.situ.web.pojo.Student;
import com.situ.web.pojo.Teacher;
import com.situ.web.service.IStudentService;
import com.situ.web.service.ITeacherService;
import com.situ.web.service.impl.StudentServiceImpl;
import com.situ.web.service.impl.TeacherServiceImpl;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
    private ITeacherDao teacherDao = new TeacherDaoImpl();
    private ITeacherService teacherService = new TeacherServiceImpl();




    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "selectAll":
                selectAll(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
        }
    }
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        if (pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "5";
        }
        PageInfo pageInfo = teacherService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        System.out.println(pageInfo);
        req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("/teacher_list.jsp").forward(req, resp);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.update");
        String id   = req.getParameter("id");
        String name = req.getParameter("name");
        String age  = req.getParameter("age");
        String gender = req.getParameter("gender");
        String salary  = req.getParameter("salary");

        Teacher teacher = new Teacher(Integer.parseInt(id), name, Integer.parseInt(age), gender,Double.parseDouble(salary));
        teacherDao.update(teacher);
        resp.sendRedirect("/teacher");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.toUpdate");
        String id = req.getParameter("id");

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Teacher teacher = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id, name, age, gender,salary FROM teacher WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, Integer.parseInt(id));
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                double salary = resultSet.getDouble("salary");
                teacher = new Teacher(Integer.parseInt(id), name, age, gender,salary);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        req.setAttribute("teacher", teacher);
        req.getRequestDispatcher("/teacher_update.jsp").forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.add");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String salary = req.getParameter("salary");

        Teacher teacher = new Teacher(name, Integer.parseInt(age), gender,Double.parseDouble(salary));
        teacherDao.add(teacher);
        resp.sendRedirect("/teacher");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.deleteById");
        String id = req.getParameter("id");

        teacherDao.deleteById(Integer.parseInt(id));

        resp.sendRedirect("/teacher");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("TeacherServlet.selectAll");
        List<Teacher> list = teacherDao.selectAll();
        req.setAttribute("list", list);
        req.getRequestDispatcher("/teacher_list.jsp").forward(req, resp);
    }
}